import { getToken, setToken, removeToken } from "@/utils/auth";
import { Message } from "element-ui";
import inter from "@/api/user";

const state = {
    token: getToken(),
    user: {},
    loading: false,
    loginShow: false
};
const mutations = {
    SET_TOKEN: (state, token) => {
        state.token = token;
    },
    SET_USER: (state, user) => {
        state.user = user;
    },
    SET_LOADING: (state, loading) => {
        state.loading = loading;
    },
    SET_LOGINSHOW: (state, loginShow) => {
        state.loginShow = loginShow;
    }
};
const actions = {
    //修改loading
    setLoading({ commit }, data) {
        commit("SET_LOADING", data);
    },
    //获取人才政策列表
    getPolicyList({ commit }, data) {
        return new Promise((resolve, reject) => {
            inter
                .getPolicyList(data)
                .then(response => {
                    resolve(response);
                })
                .catch(error => {
                    reject(error);
                });
        });
    },
    //按年度查询政策数据
    getPolicyMontn({ commit }, data) {
        return new Promise((resolve, reject) => {
            inter
                .getPolicyMontn(data)
                .then(response => {
                    resolve(response);
                })
                .catch(error => {
                    reject(error);
                });
        });
    },
    //查询人才政策标签统计列表
    getLaberCountList({ commit }, data) {
        return new Promise((resolve, reject) => {
            inter
                .getLaberCountList(data)
                .then(response => {
                    resolve(response);
                })
                .catch(error => {
                    reject(error);
                });
        });
    },
    //获取人才服务列表
    getServicetList({ commit }, data) {
        return new Promise((resolve, reject) => {
            inter
                .getServicetList(data)
                .then(response => {
                    resolve(response);
                })
                .catch(error => {
                    reject(error);
                });
        });
    },
    //获取人才计划列表
    getPlantList({ commit }, data) {
        return new Promise((resolve, reject) => {
            inter
                .getPlantList(data)
                .then(response => {
                    resolve(response);
                })
                .catch(error => {
                    reject(error);
                });
        });
    },

    // user logout
    logout({ commit }) {
        return new Promise((resolve, reject) => {
            removeToken(); // must remove  token  first
            commit("SET_TOKEN", {});
            commit("SET_USER", {});
            removeToken();
            Message({
                message: "退出登录成功",
                type: "success",
                duration: 3 * 1000
            });
            resolve();
        });
    },
    // remove token
    resetToken({ commit }) {
        return new Promise(resolve => {
            commit("SET_TOKEN", {});
            commit("SET_USER", {});
            removeToken();
            resolve();
        });
    }
};

export default {
    namespaced: true,
    state,
    mutations,
    actions
};